﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GSLMS.Entity;

namespace GSLMS.WebAPP.Areas.DianDang.Controllers.BMAudit.gManager
{
    public class gManagerController : AdminBaseController
    {
        protected override string navTabId
        {
            get { return "DianDang_BMAudit_gManager"; }
        }

        //
        // GET: /DianDang/bManager/

        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 待办事项
        /// </summary>
        /// <returns></returns>
        public ActionResult Gtasks()
        {
            var list = new List<TB_APP_SHOULDDOINFO>();
            list = serviceAppAudit.GetGMShouldDoList(CurentUser.UserName);
            
            return View(list);
        }

        /// <summary>
        /// 已办事项
        /// </summary>
        /// <returns></returns>
        public ActionResult Htasks()
        {
            return View();
        }

        #region 立项申请
        public ActionResult GProInfo(int id)
        {
            ViewData["SalesMan"] = base.GetListFromDict("");
            var model = serviceProInfo.GetSubmiShow((long)id);
            
            return View(model);
        }

        [HttpPost]
        public string GProInfo(FormCollection form)
        {
            string result = string.Empty;
            var model = new GSLMS.Entity.TB_APP_AUDIT();


            model.AUDITCONTENT = Request["AuditContent"];
            model.AUDITRESULT = GSLMS.Core.Helper.ToInt(Request["hidActiondo"]);
            model.AUDITBY = CurentUser.UserName;
            model.PROID = GSLMS.Core.Helper.ToLong(Request["id"]);
            model.AUDITTYPE = 2;



            try
            {
                bool isSumbit = false;
                isSumbit = serviceAppAudit.ProInfoBMAudit(model);
                if (isSumbit)
                { result = AjaxResultSucess; }
                else
                { result = AjaxResultFail; }
            }
            catch (Exception ex)
            {
                base.ExceptionHand(ex);
                result = AjaxResultFail;
            }

            return "{" + result + "}";
        }

        public ActionResult HProInfo(FormCollection form)
        {

            var searchParam = base.SearchPara<TB_APP_AUDIT>(form, new TB_APP_AUDIT() { AUDITTIME = DateTime.Now.AddMonths(-1), AUDITTIME_END = DateTime.Now });
            searchParam.AUDITTYPE = 2;
            searchParam.AUDITBY = CurentUser.UserName;
            var model = serviceAppAudit.GetList(searchParam) ?? new List<TB_APP_AUDIT>();
            ViewData["SearchParma"] = searchParam;
            ViewData["RowCount"] = searchParam.RowCount;
            return View(model);
        }
        #endregion

        #region 特批申请审批
        public ActionResult GSpecailInfo(int id)
        {
            ViewData["FinalApprover"] = new SelectList(serviceEmp.GetUserPrivEffList("", ""), "username", "username");
            var model = serviceSpecailInfo.GetByID(id);
            //判断是否终审用户
            ViewData["LastAuditUser"] = serviceSysAuditSet.CheckIsLastAuditUser(5, CurentUser.UserName) ? 1 : 0;
            return View(model);
        }

        [HttpPost]
        public string GSpecailInfo(FormCollection form)
        {
            string result = string.Empty;
            var model = new TB_ADU_SPECAIL();

            model.ID = 0;
            model.LOANID = 0;
            model.PROID = 0;
            model.CONID = 0;
            model.PID = Request["id"] == null ? 0 : Core.Helper.ToLong(Request["id"]);
            model.SPECIALTYPE = "5";
            model.SUBMITDEPART = CurentUser.CurrentUserInfo.ORGID;
            model.SUBMITUSER = CurentUser.UserName;
            model.REMARK = Request["Remark"] == null ? "" : Request["Remark"];
            model.TODEPART = 0;
            model.TOUSER = Request["toUser"] == null ? "" : Request["toUser"];
            model.AUDITSTATUS = Request["hidActiondo"] == null ? 0 : Core.Helper.ToInt(Request["hidActiondo"]);
            
            try
            {
                bool isSumbit = false;
                if (!serviceSysAuditSet.CheckIsLastAuditUser(5, CurentUser.UserName) && model.TOUSER == "")
                {
                    result = string.Format(ajaxStr, 300, "请选择提交到。", navTabId, "", "");
                }
                else
                {
                    isSumbit = serviceSpecail.Add(model);
                    if (isSumbit)
                    { result = AjaxResultSucess; }
                    else
                    { result = AjaxResultFail; }

                }
            }
            catch (Exception ex)
            {
                base.ExceptionHand(ex);
                result = AjaxResultFail;
            }

            return "{" + result + "}";
        }

        public ActionResult HSpecailInfo(FormCollection form)
        {
            ViewData["Product"] = new SelectList(serviceSysProduct.GetBySysetmID(Entity.Enum.SystemEnum.SystemID.典当, -1), "id", "productname");
            var searchParam = base.SearchPara<TB_ADU_SPECAIL>(form, new TB_ADU_SPECAIL() { AUDITTIME = DateTime.Now.AddMonths(-1), AUDITTIME_END = DateTime.Now });

            searchParam.SUBMITUSER = CurentUser.UserName;
            var model = serviceSpecail.GetSpecailList(searchParam) ?? new List<TB_ADU_SPECAIL>();
            ViewData["SearchParma"] = searchParam;
            ViewData["RowCount"] = searchParam.RowCount;
            return View(model);
        }
        #endregion

        #region 展期申请审批
        public ActionResult GLoanExt(int id)
        {
            ViewData["FinalApprover"] = new SelectList(serviceEmp.GetUserPrivEffList("", ""), "username", "username");
            var model = serviceLoanExt.GetByID(id);
            //判断是否终审用户
            ViewData["LastAuditUser"] = serviceSysAuditSet.CheckIsLastAuditUser(2, CurentUser.UserName) ? 1 : 0;
            return View(model);
        }

        [HttpPost]
        public string GLoanExt(FormCollection form)
        {
            string result = string.Empty;
            var model = new TB_ADU_SPECAIL();

            model.ID = 0;
            model.LOANID = 0;
            model.PROID = 0;
            model.CONID = 0;
            model.PID = Request["id"] == null ? 0 : Core.Helper.ToLong(Request["id"]);
            model.SPECIALTYPE = "2";
            model.SUBMITDEPART = CurentUser.CurrentUserInfo.ORGID;
            model.SUBMITUSER = CurentUser.UserName;
            model.REMARK = Request["Remark"] == null ? "" : Request["Remark"];
            model.TODEPART = 0;
            model.TOUSER = Request["toUser"] == null ? "" : Request["toUser"];
            model.AUDITSTATUS = Request["hidActiondo"] == null ? 0 : Core.Helper.ToInt(Request["hidActiondo"]);

            try
            {
                bool isSumbit = false;
                if (!serviceSysAuditSet.CheckIsLastAuditUser(5, CurentUser.UserName) && model.TOUSER == "")
                {
                    result = string.Format(ajaxStr, 300, "请选择提交到。", navTabId, "", "");
                }
                else
                {
                    isSumbit = serviceSpecail.Add(model);
                    if (isSumbit)
                    { result = AjaxResultSucess; }
                    else
                    { result = AjaxResultFail; }

                }
            }
            catch (Exception ex)
            {
                base.ExceptionHand(ex);
                result = AjaxResultFail;
            }

            return "{" + result + "}";
        }

        public ActionResult HLoanExt(FormCollection form)
        {
            ViewData["Product"] = new SelectList(serviceSysProduct.GetBySysetmID(Entity.Enum.SystemEnum.SystemID.典当, -1), "id", "productname");
            var searchParam = base.SearchPara<TB_LOAN_MAIN>(form, new TB_LOAN_MAIN() { APPLYDATE = DateTime.Now.AddMonths(-1), APPLYDATE_END = DateTime.Now });
            var model = serviceLoanMain.GetList(searchParam) ?? new List<TB_LOAN_MAIN>();
            ViewData["SearchParma"] = searchParam;
            ViewData["RowCount"] = searchParam.RowCount;
            return View(model);
        }
        #endregion

        #region 费用减免审批
        public ActionResult GFeesFree(int id)
        {
            ViewData["FinalApprover"] = new SelectList(serviceEmp.GetUserPrivEffList("", ""), "username", "username");
            var model = serviceFeesFree.GetByID(id);
            //判断是否终审用户
            ViewData["LastAuditUser"] = serviceSysAuditSet.CheckIsLastAuditUser(6, CurentUser.UserName) ? 1 : 0;
            return View(model);
        }

        [HttpPost]
        public string GFeesFree(FormCollection form)
        {
            string result = string.Empty;
            var model = new TB_ADU_SPECAIL();

            model.ID = 0;
            model.LOANID = 0;
            model.PROID = 0;
            model.CONID = 0;
            model.PID = Request["id"] == null ? 0 : Core.Helper.ToLong(Request["id"]);
            model.SPECIALTYPE = "6";
            model.SUBMITDEPART = CurentUser.CurrentUserInfo.ORGID;
            model.SUBMITUSER = CurentUser.UserName;
            model.REMARK = Request["Remark"] == null ? "" : Request["Remark"];
            model.TODEPART = 0;
            model.TOUSER = Request["toUser"] == null ? "" : Request["toUser"];
            model.AUDITSTATUS = Request["hidActiondo"] == null ? 0 : Core.Helper.ToInt(Request["hidActiondo"]);

            try
            {
                bool isSumbit = false;
                if (!serviceSysAuditSet.CheckIsLastAuditUser(5, CurentUser.UserName) && model.TOUSER == "")
                {
                    result = string.Format(ajaxStr, 300, "请选择提交到。", navTabId, "", "");
                }
                else
                {
                    isSumbit = serviceSpecail.Add(model);
                    if (isSumbit)
                    { result = AjaxResultSucess; }
                    else
                    { result = AjaxResultFail; }

                }
            }
            catch (Exception ex)
            {
                base.ExceptionHand(ex);
                result = AjaxResultFail;
            }

            return "{" + result + "}";
        }

        public ActionResult HFeesFree(FormCollection form)
        {
            ViewData["Product"] = new SelectList(serviceSysProduct.GetBySysetmID(Entity.Enum.SystemEnum.SystemID.典当, -1), "id", "productname");
            var searchParam = base.SearchPara<TB_LOAN_MAIN>(form, new TB_LOAN_MAIN() { APPLYDATE = DateTime.Now.AddMonths(-1), APPLYDATE_END = DateTime.Now });
            var model = serviceLoanMain.GetList(searchParam) ?? new List<TB_LOAN_MAIN>();
            ViewData["SearchParma"] = searchParam;
            ViewData["RowCount"] = searchParam.RowCount;
            return View(model);
        }

        #endregion


    }
}
